package com.tevintao.marketingsystem.task;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;

import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByClassName;
import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByIdAndContent;


public class AddNearbyFriendTaskProcess extends SetTaskExtra {
    private static AddNearbyFriendTaskProcess ourInstance = new AddNearbyFriendTaskProcess();

    public static AddNearbyFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private AddNearbyFriendTaskProcess() {
    }

    private Boolean ifPassNoLoaction = false;//是否按下  跳过按钮   使   无法定位提示框跳过
    private Boolean ifSelectSex = false;//是否已经选择了查看  男  女   或者全部
    private Boolean ifCall = false;//是否打过招呼了。是的话，返回附近人列表，没有的话就 接着打招呼
    private int mSelectPosition = -1;
    private Random mRandom = new Random();

    private int failedNum = 0;
    private int hasaddnearbyfriendNum = 0;//本次任务  已经添加的附近人数量
    private int addnearbyfriendgoalNum = 1;//mRandom.nextInt(11)+5;//添加附近人的  数量    如果大于此变量   任务结束
    private static final int MAX_FAILED_NUM = 3;
    private String nikeName = "";

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        LogManager.getInstance().logInfo("BrowFriendTaskProcess processView contextName = " + contextName + " v = " + v.toString());

        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            LogManager.getInstance().logInfo("intab = " + inDiscover);
            if (inDiscover) {
                View nearbyView = getViewByIdAndContent((ViewGroup) v, 16908310, "附近的人");
                LogManager.getInstance().logInfo("nearbyView = " + nearbyView);
                if (null != nearbyView) {
                    ViewOperation.clickView(nearbyView);
                    ifPassNoLoaction = false;
                }
            }
            //mSelectPosition = -1;
        } else if ("android.view.ContextThemeWrapper".equals(contextName)) {
            View nolocationView = getViewByIdAndContent((ViewGroup) v, 2131626642, "设置");
            View notishiView = v.findViewById(2131626201);
            View paseView = v.findViewById(2131626641);
            if (nolocationView != null && notishiView != null && paseView != null) {
                LogManager.getInstance().logInfo("微信  弹出  无法定位 提示框");
                ViewOperation.clickView(paseView);
                ifPassNoLoaction = true;//每次进入附近的人 重新设置为fasle
                LogManager.getInstance().logInfo("跳过了提示框");
            } else {
                View checkboxView = v.findViewById(2131626198);
                if (null != checkboxView && checkboxView instanceof CheckBox) {
                    LogManager.getInstance().logInfo("发现了  提示弹窗页面  提示查看附近的人功能将获取你的位置信息，你的位置信息会被保留一段时间。通过列表右上角的清除功能可随时手动清除位置信息");
                    CheckBox checkbox = (CheckBox) checkboxView;
                    ViewOperation.clickView(checkbox);
                    LogManager.getInstance().logInfo("勾选了  下次不再提示框");
                }

                View sureView = getViewByIdAndContent((ViewGroup) v, 2131626642, "确定");
                //View sureView = v.findViewById(2131626642);
                if (null != sureView && sureView instanceof Button) {
                    Button surebutton = (Button) sureView;
                    ViewOperation.clickView(surebutton);
                    LogManager.getInstance().logInfo("点击确定按钮    下次不会再弹出提示框");
                }
            }
        } else if (WxUiViewConfig.VIEW_NEARBY_OTHER_FRIEND_SAY_HI.equals(contextName)) {
            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View backView = v.findViewById(WxUiViewConfig.KEY_NEARBY_BACK_VIEW);
            if (null != backView) {
                ViewOperation.clickView(backView);
            }
        } else if (WxUiViewConfig.VIEW_NEARBY_SAY_HI.equals(contextName)) {
            View sayHi = v.findViewById(WxUiViewConfig.KEY_NEARBY_SAY_HI_FRIEND);
            if (null != sayHi && sayHi.getVisibility() == View.VISIBLE) {
                ViewOperation.clickView(sayHi);
            } else {
                View lookNearby = v.findViewById(WxUiViewConfig.KEY_NEARBY_LOOK_BUTTON_VIEW);
                if (null != lookNearby) {
                    ViewOperation.clickView(lookNearby);
                }
            }
        } else if ("com.tencent.mm.plugin.nearby.ui.NearbyFriendsIntroUI".equals(contextName)) {
            View beginlookView = getViewByIdAndContent((ViewGroup) v, 2131626837, "开始查看");
            if (beginlookView != null) {
                ViewOperation.clickView(beginlookView);
            }
        } else if (WxUiViewConfig.VIEW_NEARBY_LIST_VIEW.equals(contextName)) {
            // View searchView = getViewByIdAndContent((ViewGroup) v,2131626835, "暂时没有找到附近也使用该功能的人，请稍后再尝试查看。");

            View failedView = v.findViewById(WxUiViewConfig.KEY_NEARBY_FAILED_VIEW);
            if (null != failedView && failedView.getVisibility() == View.VISIBLE) {
                LogManager.getInstance().logInfo("定位失败页面");
                failedNum++;
                if (failedNum > MAX_FAILED_NUM) {
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_LOCATION_ERROR, "多次定位失败");
                    return true;
                } else {
                    View backView = v.findViewById(WxUiViewConfig.KEY_NEARBY_BACK_VIEW);
                    if (null != backView) {
                        ViewOperation.clickView(backView);
                    }
                    return true;
                }
            }


            WxUiViewConfig.printViewGroup((ViewGroup) v);


            ViewGroup searchView = (ViewGroup) v.findViewById(2131626834);
            //LogManager.getInstance().logInfo("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+searchView.getChildCount());
            //if (searchView!=null)

            if (searchView == null || searchView != null && (searchView.getChildCount() != 0 && searchView.getChildCount() != 1)) {

                LogManager.getInstance().logInfo("来没来");
                View selectSex = getViewByClassName((ViewGroup) v, "ActionMenuItemView");
                if (ifSelectSex == false) {
                    if (null != selectSex) {
                        ViewOperation.clickView(selectSex);
                    }

                    View selectView = v.findViewById(2131624112);
                    if (null != selectView) {
                        switch (0) {
                            case 0:
                                selectView = getViewByIdAndContent((ViewGroup) v, 2131624112, "查看全部");
                                LogManager.getInstance().logInfo("选择了查看全部");
                                break;
                            case 1:
                                selectView = getViewByIdAndContent((ViewGroup) v, 2131624112, "只看男生");
                                LogManager.getInstance().logInfo("选择了只看男生");
                                break;
                            case 2:
                                selectView = getViewByIdAndContent((ViewGroup) v, 2131624112, "只看女生");
                                LogManager.getInstance().logInfo("选择了只看女生");
                                break;
                        }
                        ifSelectSex = true;
                        ViewOperation.clickView(selectView);
                    }
                } else {
                    LogManager.getInstance().logInfo("当前位于附近人好友列表");
                    if (hasaddnearbyfriendNum >= addnearbyfriendgoalNum)
                        taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "已经添加了   所需的" + addnearbyfriendgoalNum + "个好友  结束添加附近人好友的任务");
                    View listView = v.findViewById(2131626834);
                    LogManager.getInstance().logInfo("listView = " + listView);
                    if (null != listView && listView instanceof ListView) {
                        ListView list = (ListView) listView;
                        if (mSelectPosition < 0) {
                            //（mSelectPosition的取值范围为1到list.getAdapter().getCount()-list.getHeaderViewsCount） 即是从第一个好友到最后一个好友
                            mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 1) + list.getHeaderViewsCount();
                        }
                        int first = list.getFirstVisiblePosition();
                        int last = list.getLastVisiblePosition();
                        LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + list.getAdapter().getCount() + "  " + list.getCount() + " " + list.getFooterViewsCount());
                        if (mSelectPosition < first || mSelectPosition > last) {
                            ViewOperation.moveListPosition(list, mSelectPosition);
                            LogManager.getInstance().logInfo("不到位");
                            WxUiViewConfig.printViewGroup((ViewGroup) v);
                        } else {
                            mSelectPosition = -1;
                            LogManager.getInstance().logInfo("移动到位");

                            View firstvisibleView = list.getChildAt(0);
                            WxUiViewConfig.printViewGroup((ViewGroup) v);
                            //JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree((ViewGroup) firstView, -1);
                            //HookWindowManagerGlobal.printViewTreeJson(joTree, 0);


                            if (null != firstvisibleView) {
                                View textViewname = v.findViewById(2131624466);
                                if (textViewname instanceof TextView && textViewname != null) {
                                    TextView textView = (TextView) textViewname;
                                    nikeName = textView.getText().toString();
                                    String nikenameFirstLetter = nikeName.substring(0, 1);
                                    if (!"".equals(nikenameFirstLetter)) {
                                        if ("a".equals(nikenameFirstLetter) || "A".equals(nikenameFirstLetter))
                                            LogManager.getInstance().logInfo("此微信号码昵称首字母为a 或者A  通常是做营销的    跳过重新移动位置");
                                        else
                                            ViewOperation.clickView(firstvisibleView);
                                    }
                                }
                                //ViewOperation.clickView(firstvisibleView);
                            }
                        }
                    }
                }
            } else {
                LogManager.getInstance().logInfo("whywhywhywhywhywhywhywhywhywhywhywhywhywhywhywhywhywhy+1");
                if (ifPassNoLoaction == true) {
                    View backView = v.findViewById(2131624160);
                    if (null != backView) {
                        ViewOperation.clickView(backView);
                        LogManager.getInstance().logInfo("点击返回按钮    返回到了发现界面");
                    }
                } else {
                    LogManager.getInstance().logInfo("正在确定你的位置");
//                            WxUiViewConfig.printViewGroup((ViewGroup) v);
//                            LogManager.getInstance().logInfo("正在确定你的位置  后");
                }
            }
        } else if ("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(contextName)) {
            if (ifCall == false) {
                LogManager.getInstance().logInfo("点击了附近人列表中的人    进入了打招呼界面");
                //首先找到打招呼按钮
                View buttonViewCall = v.findViewById(2131625116);
                if (null != buttonViewCall && buttonViewCall instanceof Button) {
                    ViewOperation.clickView(buttonViewCall);
                }
                ifCall = true;
            } else {
                LogManager.getInstance().logInfo("发送了加人请求后    返回了打招呼界面  如果对方没有设置好友验证 也返回到同一个 activty (但是界面内容稍有不同) 并且直接成为了好友");
                //找到返回按钮   按下后会返回附近人好友列表
                View buttonViewBack = v.findViewById(2131624160);
                if (null != buttonViewBack) {
                    ViewOperation.clickView(buttonViewBack);
                }
                ifCall = false;
                LogManager.getInstance().logInfo("按下了返回按钮");
            }
            WxUiViewConfig.printViewGroup((ViewGroup) v);

        } else if ("com.tencent.mm.ui.contact.SayHiEditUI".equals(contextName)) {
            LogManager.getInstance().logInfo("打招呼内容输入界面");
            WxUiViewConfig.printViewGroup((ViewGroup) v);

            View editViewShuRu = v.findViewById(2131627356);
            if (editViewShuRu instanceof EditText) {
                EditText textView = (EditText) editViewShuRu;
                if ("".equals(textView.getText().toString())) {
                    //TextView textView = (TextView) buttonViewFaSong;
                    LogManager.getInstance().logInfo("等待自动输入评论内容 ");
                    String[] str = new String[]{
                            "我是猪？", "老同学在哪发财呢？", "好久不见了啊", "Holle", "在干嘛，约吗？", "我好像在哪见过你", "你怎么也来这边了", "忙什么呢",
                            "干嘛呢，那天在街上看到你都不理我！", "暗恋你这么久了，今天才有勇气加你。", "明天下午六点老地方不见不散。", "你电话怎么打不通？", "看你图像挺特别的，你经常照这样的照片吗？", "看你朋友圈，觉得你挺性感的，是我喜欢的类型。", "认识我绝对是一种美好的开始，我相信，你会懂的", "你的世界如此苍白，我来点缀一下", "我从天堂到地狱路过人间，顺便和你打声招呼", "通常没有任何介绍的人，都是不自信的人,我觉得你不应该啊",
                            "你即将错过一个让你生命更精彩的人。", "前世五百次的回眸才换来今生相见？", "你知道我最近是怎么过的吗？", "为什么每次打电话你都不回我", "为什么要删掉我，我真的不明白为什么", "难道在你心里我就这么小气嘛", "你知道不知道我心里是多么的难过", "不理我，不理我，哼.....", "在你的心里，我就是那么的不重要是么?", "即使是这样，我也毫不犹豫的去爱你，可你呢？",
                            "你的一颦一笑都在我心里荡起涟漪。", "离开我你真的过得很好吗？", "我之所以流泪，是因为在乎。", "如果真的有一天我不在你身边了，你会想我吗？", "有些事，明知道是错的，却还要坚持", "怕被伤害的人，永远抓不到真正的幸福", "如果我不在乎，我不会再为你笑；可你呢？", "我怀念的不是你，而是你给的致命曾经", "曾经海枯石烂，抵不过好聚好散。", "三生石，三生已过，来世我们还会再相见吗？",
                            "每个人总会找到一个让你心甘情愿傻傻相伴的人，是你吗？", "你的过去我来不及参与，你的未来我奉陪到底。", "聚散都是缘，离别总关情。", "泪水是我想你的滋味，我寄出的心无力挽回", "寂寞人生爱无休，寂寞是爱永远的主题", "我会用一生珍惜这个结局，因为你曾给过我“美丽心情”！", "我和我的影子都在想你，可是你在哪里。", "思念偷偷地逃出，每天想你", "不知道是否会找到你，告诉你我曾经爱的好美", "如果回忆是唯一的回信，我不会忘记我曾经美丽。",
                            "我愿做你眼里的一滴泪，当你把我哭出来的时候，让我在你的唇边消失。", "我知道我该收起这份失落，可最在乎的牵挂已涌入心窝。", "我失落，彷徨的时候你在哪里？", "也许没有结局的结局，才是最好的结局？", "我唯一没有做好的事，就是说了“我爱你”。", "转弯只为遇见你，却忘记了，你也会转弯。", "也许有一天，你回头了，而我却早已，不在那个路口", "我再也不会奋不顾身的去爱一个人了，哪怕是你", "世界上最遥远的距离，是你转身后，我眼泪坠落的轨迹。", "没有他我不会不习惯，因为我从来没有习惯拥有他。"
                    };
                    ViewOperation.setText(textView, nikeName + "，" + str[mRandom.nextInt(58)]);
                } else {
                    View enterView = v.findViewById(2131624139);

                    TextView enterbutton = (TextView) enterView;
                    if ("发送".equals(enterbutton.getText().toString())) {
                        ViewOperation.clickView(enterbutton);
                        LogManager.getInstance().logInfo("自动输入打招呼内容之后，按下了发送按钮");
                        hasaddnearbyfriendNum++;
                        LogManager.getInstance().logInfo("在附近人中添加了第" + hasaddnearbyfriendNum + "个好友");
                    }
                }
            }

        } else if (WxUiViewConfig.VIEW_ADD_AREA.equals(contextName)) {

            View female = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_FEMALE, "女");
            if (null != female) {
                if (female instanceof RadioButton) {
                    RadioButton button = (RadioButton) female;
                    if (!button.isChecked()) {
                        ViewOperation.clickView(female);
                    }
                }
            }


            View showCity = v.findViewById(WxUiViewConfig.VIEW_SHOW_CITY);
            if (null != showCity && showCity.getVisibility() == View.VISIBLE && showCity instanceof TextView) {
                TextView selectedCity = (TextView) showCity;
                LogManager.getInstance().logInfo("selectedCity.getText().toString() = " + selectedCity.getText().toString());
                if (!TextUtils.isEmpty(selectedCity.getText().toString().trim())) {
                    LogManager.getInstance().logInfo("定位成功");
//                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    View noSign = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_SELECT_SIGN, "未填写");
                    if (null != noSign) {
                        ViewOperation.clickView(noSign);
                        LogManager.getInstance().logInfo("需要签名");
                        return true;
                    }
                    View nextStep = v.findViewById(WxUiViewConfig.VIEW_SELECT_CITY_NEXT);
                    if (null != nextStep) {
                        ViewOperation.clickView(nextStep);
                    }
                    return true;
                }
            }

            View area = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_AREA, "地区*");
            LogManager.getInstance().logInfo("area = " + area);
            if (null != area) {
                ViewOperation.clickView(area);
            }


        } else if (WxUiViewConfig.VIEW_SELECT_CITY.equals(contextName)) {
            View dingwei = v.findViewById(WxUiViewConfig.VIEW_SELECT_CITY_LOCATION);
            LogManager.getInstance().logInfo("dingwei view = " + dingwei);
            if (null != dingwei) {
                if (dingwei.getVisibility() == View.VISIBLE) {
                    if (dingwei instanceof TextView) {
                        TextView dingweiText = (TextView) dingwei;
                        if ("无法获取你的位置信息".equals(dingweiText.getText().toString())) {
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_LOCATION_ERROR, "定位失败");
                            return true;
                        }
                    }
                    LogManager.getInstance().logInfo("定位中");
                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    return true;
                }
                ViewParent father = dingwei.getParent();
                LogManager.getInstance().logInfo("father ViewParent = " + father);
                if (null != father && father instanceof View) {
                    View fatherView = (View) father;
                    LogManager.getInstance().logInfo("fatherView view = " + fatherView);
                    ViewOperation.clickView(fatherView);
                }
            }
        } else if (WxUiViewConfig.VIEW_EDIT_SIGNATURE.equals(contextName)) {
            String testSign = "开心每一天";

            View inputSign = v.findViewById(WxUiViewConfig.INPUT_SIGN_EDIT);
            if (inputSign instanceof EditText) {
                TextView singTextView = (TextView) inputSign;
                String signContext = singTextView.getText().toString();
                if (!signContext.equals(testSign)) {
                    ViewOperation.setText(singTextView, testSign);
                } else {
                    View saveSign = v.findViewById(WxUiViewConfig.SAVE_SIGN_BUTTON);
                    if (null != saveSign) {
                        ViewOperation.clickView(saveSign);
                    }
                }
            }
        }


        return false;
    }
}
